# import cv2
import os
import numpy as np
# import albumentations as A
import time
from PIL import Image, ImageEnhance


def adjust_brightness_contrast_PIL(input_folder, output_folder, brightness_factor=1.0, contrast_factor=1.0):
    # 读取图像

    for filename in os.listdir(input_folder):
        if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".bmp"):
            # print(filename)
            image_path = os.path.join(input_folder, filename)
            print(image_path)

            # image = cv2.imread(image_path)
            # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            img = Image.open(image_path)
            enhancer = ImageEnhance.Brightness(img)
            img_brightened = enhancer.enhance(brightness_factor)

            # 调整对比度
            enhancer = ImageEnhance.Contrast(img_brightened)
            img_contrasted = enhancer.enhance(contrast_factor)

            # changed_image = adjust_brightness_contrast_PIL(img, brightness_factor=1, contrast_factor=1)
            if not os.path.exists(output_folder):
                os.makedirs(output_folder)
            image_path1 = os.path.join(output_folder, filename)
            img_contrasted.save(image_path1)
            print(image_path1)
    print('done!---------------------')
    # 调整亮度
    # enhancer = ImageEnhance.Brightness(img)
    # img_brightened = enhancer.enhance(brightness_factor)

    # 调整对比度
    # enhancer = ImageEnhance.Contrast(img_brightened)
    # img_contrasted = enhancer.enhance(contrast_factor)

    # return img_contrasted


# # 使用：
# adjusted_img = adjust_brightness_contrast_PIL("path_to_your_image.jpg", 1.2, 1.2)
# adjusted_img.show()


def show_picinfo(image_path):
    # print('show------------')
    # print(image_path)
    # print('---------------------')
    image = Image.open(image_path).convert("L")
    image_data = np.array(image)

    brightness = np.mean(image_data)
    contrast = np.std(image_data)

    print(f"Brightness: {brightness}")
    print(f"Contrast: {contrast}")

if __name__ == '__main__':

    input_folder = "E:/Dataset/ZDATAaug/images/test"
    output_folder = "E:/Dataset/ZDATAaug/new/images"
    path = 'E:\Dataset\ZDATAaug/new\images'
    #adjust_brightness_contrast_PIL(input_folder, output_folder, brightness_factor=1.2, contrast_factor=1.0)
    # time.sleep(0.5)

    for filename in os.listdir(path):
        if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".bmp"):
            print(filename)
            image_path = os.path.join(path, filename)
            print(f'文件：{image_path}')
            show_picinfo(image_path)
            print('------------------\n')
